package com.doge;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class SkillupScreen extends Stage implements Screen, InputProcessor
{
	Main parent = null;
	Player player = null;
	
	SpriteBatch batch = new SpriteBatch();
	public BitmapFont font = new BitmapFont();
	public String statusText = "";
	
	int activeButton = 0;
	
	class Button
	{
		String name;
		int str = 0;
		int magic = 0;
		boolean RandomAbiltiy;
		int cost = 0;
		
		Button(String name, int str, int magic, boolean rng, int cost)
		{
			this.name = name;
			this.str = str;
			this.magic = magic;
			this.RandomAbiltiy = rng;
			this.cost = cost;
		}
	}
	
	public SkillupScreen (Main p)
	{
		parent = p;
		player = p.p;

		buttons.add(new Button("Add Strength",1,0,false,5*player.str));
		buttons.add(new Button("Add Magic",0,1,false,5*player.magic));
		buttons.add(new Button("Add Random Abiltiy",0,0,true,5*(player.battleActions.size() - player.unlearnedActions.size())));
	}
	
	ArrayList<Button> buttons = new ArrayList<Button>();
	
	@Override
	public void render(float delta) 
	{
		batch.begin();
		batch.draw(Textures.outline, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		font.draw(batch, statusText, Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/3);
		
		for(int x = 0; x < buttons.size(); ++x)
		{
			float padding = 20;
			float xpos = Gdx.graphics.getWidth()/2;
			float ypos = (15*x)+font.getXHeight()+(Gdx.graphics.getHeight()/2);
			String name = buttons.get(x).name;
			
			//draw arrow
			if(x == activeButton)
			{
				batch.draw(parent.textures.arrowRight,xpos-70, 
						ypos-10, 
						64, 
						36);
			}
			
			//draw 'buttons'
			font.setColor(Color.WHITE);
			
			font.draw(batch, name, xpos, ypos+15);
		}
		batch.end();
	}
	
	void applyStat()
	{
		Button b = buttons.get(this.activeButton);
		if(player.dogeCoins < b.cost)
		{
			statusText = "Not Enough Doge Coints";
			return;
		}
		
		
		
		if(b.str > 0){
			//player.str += b.str;
			player.addStat(1, 0);
			b.cost = 5*player.str;
		}else if (b.magic > 0){
			//player.magic += b.magic;
			player.addStat(1, 1);
			b.cost = 5*player.magic;
		}
		
		
		
		String ability = "";
		if(b.RandomAbiltiy )
		{
			if(player.unlearnedActions.size() > 0)
				statusText = "No more abilites to learn!";
			
			b.cost = 5*(player.battleActions.size() - player.unlearnedActions.size());
			int x = (int)Math.random()*player.unlearnedActions.size();
			player.battleActions.add(player.unlearnedActions.get(x));
			player.unlearnedActions.remove(x);
			ability = "New Ability = " + player.battleActions.get(player.battleActions.size()-1).name;
		}
		
		player.dogeCoins -= b.cost;
		statusText = "str = "+player.str+ " magic = "+player.magic+ " " +ability;
	}
	
	public boolean keyUp(int keycode) 
	{	
		switch(keycode)
		{
		case Keys.W:
			if(++activeButton > buttons.size()-1 )
				activeButton = 0;
			return true;
		case Keys.S:
			if(--activeButton < 0 )
				activeButton = buttons.size()-1;
			return true;
		case Keys.ENTER:
			applyStat();
			return true;
		case Keys.ESCAPE:
			this.dispose();
			return true;
		}
		
        return false;
    }
	
	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}
	
	public void dispose() 
	{
		Gdx.input.setInputProcessor(parent.overworld);
		parent.setScreen(parent.overworld);
	}

}
